package com.yjhb.android.sbz.mvp.settingtimer;

import com.yjhb.android.sbz.entity.Timer;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;
import com.yjhb.android.sbz.tool.Tool;

/**
 * 创建者 zx
 * 创建时间 2021/1/12 17:08
 */
public class SettingTimerPresenter extends ConventionPresenter<SettingTimerView> {
    private final String TAG = "SettingTimerPresenter";

    public SettingTimerPresenter(SettingTimerView settingTimerView) {
        super(settingTimerView);
    }

    @Override
    protected void onDestroy() {

    }

    public void addTimer(Timer timer) {
        if (view != null) {
            view.onWait("正在添加...");
        }
        HttpTool.request(model.addTimer(Tool.entityToMap(timer)), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(0,data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    public void updateTimer(Timer timer) {
        if (view != null) {
            view.onWait("正在修改...");
        }
        HttpTool.request(model.updateTimer(Tool.entityToMap(timer)), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(1,data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    public void changeState(Integer id, Integer state) {
        if (view != null) {
            view.onWait("正在停止...");
        }
        HttpTool.request(model.updateTimerState(id, state), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(2,state);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


}
